<template>
    <div>
        <Modal v-model="isShowModal" :title="title" @on-cancel="hide('formValidate')" :mask-closable="false" width="800">

            <Form ref="formValidate" :model="params" :rules="ruleValidate" label-position="top">
                <FormItem label="消息标题：" prop="msgTitle">
                    <Input v-model.trim="params.msgTitle" placeholder="请输入消息标题"></Input>
                </FormItem>
                <FormItem label="消息类型：" prop="msgType">
                    <Select v-model="params.msgType" placeholder="选择消息类型">
                        <Option value="1">公告消息</Option>
                        <Option value="2">系统消息</Option>
                    </Select>
                </FormItem>
                <FormItem label="消息内容：" prop="msgContent">
<!--                    <Input v-model.trim="params.msgContent" type="textarea" :rows="4" placeholder="请输入消息内容"></Input>-->
                    <hs-edit ref="edit" id="0" v-if="isEditShow" :value="params.msgContent"
                             @edit-out="editOut"></hs-edit>
                </FormItem>
            </Form>

            <!-- 重写对话框底部按钮 -->
            <div slot="footer">
                <Button @click="hide('formValidate')">取消</Button>
                <Button v-if="type != 1" type="primary" @click="handleSubmit('formValidate')" :loading="loadingBtn">
                    <span v-if="!loadingBtn">确定</span>
                    <span v-else>请稍候...</span>
                </Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import hsEdit from '@/components/hs-edit';
    export default {
        data() {
            return {
                isShowModal: false,
                loadingBtn: false,
                isEditShow: false,
                title: '查看信息',
                type: 0,
                params: {
                    msgTitle: '',
                    msgContent: '',
                    msgType: '',
                    messageId: ''
                },
                ruleValidate: {
                    msgTitle: [
                        {required: true, message: '消息标题不能为空', trigger: 'change'}
                    ],
                    msgContent: [
                        {required: true, message: '消息内容不能为空', trigger: 'change'}
                    ],
                    msgType: [
                        {required: true, message: '消息类型不能为空', trigger: 'change', type: 'string'}
                    ]
                }
            }
        },
        methods: {
            // 点击提交验证
            handleSubmit(name) {
                // console.log(this.params);
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.$Message.error('验证通过');
                        /**
                         * type判断
                         * 1.查看 2.新增 3.编辑
                         */
                        switch (this.type) {
                            case 1:
                                // this.title = '查看消息';
                                break;
                            case 2:
                                // 新增消息
                                this.addMessage();
                                break;
                            case 3:
                                // 编辑消息
                                this.updateMessage();
                                break;
                            default:
                                this.$Notice.error({
                                    title: '消息管理',
                                    desc: '查看，新增，编辑区分判断报错',
                                    duration: 0
                                });
                                break
                        }
                    } else {
                        this.$Message.error('还有信息没有填写合格哦~');
                    }
                })
            },
            // 显示模态框
            show(type, row) {
                this.isShowModal = true;
                this.isEditShow = true;
                this.type = type;
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                switch (type) {
                    case 1:
                        this.title = '查看消息';
                        this.getInfo(row);
                        break;
                    case 2:
                        this.title = '新增消息';
                        break;
                    case 3:
                        this.title = '编辑消息';
                        this.getInfo(row);
                        break;
                    default:
                        this.$Notice.error({
                            title: '消息管理',
                            desc: '查看，新增，编辑模态框报错',
                            duration: 0
                        });
                        break
                }
            },
            // 新增消息
            addMessage() {
                this.$Spin.show();
                this.axios.post('/message/addMessage', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '新增消息接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 编辑消息
            updateMessage(){
                this.$Spin.show();
                this.axios.post('/message/updateMessage', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeDate');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '编辑消息接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 查询消息详情
            getInfo(row){
                this.$Spin.show();
                this.axios.post('/message/getInfo', {
                    messageId: row.messageId
                }).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        // this.$Message.success('操作成功.');
                        this.params.msgTitle = res.data.msgTitle;
                        this.params.msgContent = res.data.msgContent;
                        this.params.msgType = res.data.msgType.toString();
                        this.params.messageId = res.data.messageId;
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查看消息详情接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 富文本编辑器返回的内容
            editOut(e) {
                this.params.msgContent = e;
            },
            // 关闭模态框
            hide(name) {
                this.isShowModal = false;
                this.isEditShow = false;
                this.params.messageId = '';
                this.$refs[name].resetFields();
            }
        },
        components:{
            hsEdit
        }
    }
</script>

<style scoped>

</style>